//
//  YTFOriginalView.swift
//  sinaWeibo
//
//  Created by pro on 16/3/1.
//  Copyright © 2016年 pro. All rights reserved.
//

import UIKit
import SnapKit

let CellMargin = 10
class YTFOriginalView: UIView {
    
    //定义一个属性来记录约束
    var bottomConstraint: Constraint?
    
    //定义一个属性
    var statusViewModel: YTFStatusViewModel?{
        didSet{
            //微博名称
            nameLabel.text = statusViewModel?.statusModel?.user?.screen_name
//            print(statusViewModel?.statusModel?.created_at)
            //微博内容
            contentLabel.text = statusViewModel?.statusModel?.text
            //微博认证
            verifyImageView.image = statusViewModel?.verify_image
//            VIP等级
            levelImageView.image = statusViewModel?.level_image
            //头像
            photoImageView.sd_setImageWithURL(NSURL(string: statusViewModel?.statusModel?.user?.profile_image_url ?? ""))
            
//            //创建时间
            timeLabel.text = statusViewModel?.dealCreateTime(statusViewModel?.statusModel?.created_at)
            
//            //微博来源
            sourceLabel.text = statusViewModel?.dealSource(statusViewModel?.statusModel?.source)
            
            //在开始判断之前清空约束，避免造成约束加重
            bottomConstraint?.uninstall()
            
            //判断是否有配图
            if let pic_urls = statusViewModel?.statusModel?.pic_urls where pic_urls.count > 0{
//                print(statusViewModel?.statusModel?.text)
                //显示
                pictureView.hidden = false
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                   bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                //传值
//                pictureView.pictureViewModel = statusViewModel
                
                //定义一个临时数组进行数据拼接
                var result = [YTFPictureViewModel]()
                
                //通过遍历原创微博的配图视图数组，把YTFPictureModel传递给YTFPictureViewModel
                for model in pic_urls{
                    let viewModel = YTFPictureViewModel()
                    viewModel.model = model
                    result.append(viewModel)
                }
                
                pictureView.pictureList = result
                
            }else{
                //隐藏
                pictureView.hidden = true
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
            
        }
    
    }
    
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加视图并设置约束
    private func setupUI(){
        
        //添加控件
        addSubview(photoImageView)
        addSubview(verifyImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        //头像
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(CellMargin)
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.size.equalTo(CGSizeMake(40, 40))
//            make.bottom.equalTo(self.snp_bottom)
            
        }
        
        //认证图片
        verifyImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(photoImageView.snp_right)
            make.bottom.equalTo(photoImageView.snp_bottom)
        }
        
        //微博昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(photoImageView.snp_top)
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
        }
        
        //会员等级
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel.snp_centerY)
            make.left.equalTo(nameLabel.snp_right).offset(CellMargin)
        }
        
        //发布时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_bottom).offset(CellMargin / 2)
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
        }
        
        //发布来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
        }
        
        //微博正文
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_bottom).offset(CellMargin)
            
            //设置宽度
            make.width.equalTo(kUIScreenWidth - 2*CGFloat(CellMargin))
                
//            make.bottom.equalTo(self.snp_bottom).offset(-CellMargin)
        }
        
        //配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        //设置最底下的约束
        self.snp_makeConstraints { (make) -> Void in
            //把这个约束赋值给属性
//            make.bottom.equalTo(pictureView.snp_bottom)
            bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(CellMargin).constraint
        }
        
    }
    
    
    //懒加载
    //头像
    private lazy var photoImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        img.layer.cornerRadius = 20

        img.layer.masksToBounds = true
        
        return img
    }()
    
    
    //认证图标
    private lazy var verifyImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_vip")
        
        return img
    }()
    
    
    //微博昵称
    private lazy var nameLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "微博昵称"
        
        label.font = UIFont.systemFontOfSize(15)
        
        return label
    }()
    
    //VIP等级
    private lazy var levelImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_expired")
    
        return img
    }()
    
    //发布时间
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "发布时间"
        
        label.font = UIFont.systemFontOfSize(12)
        
        label.textColor = UIColor.orangeColor()
        
        return label
    }()
    
    //微博来源
    private lazy var sourceLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "微博来源"
        
        label.textColor = UIColor.darkGrayColor()

        label.font = UIFont.systemFontOfSize(12)
        
        return label
    }()
    
    
    //微博正文
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "微博正文"
        
        label.font = UIFont.systemFontOfSize(15)
        
        //换行
        label.numberOfLines = 0
        
        return label
    }()
    
    //配图
    private lazy var pictureView: YTFPictureView = YTFPictureView()
    
}







